#include "Scorer.h"

class Scorer2004 : public Scorer {
    private:
            /*
         Returns given node's relative area and aspect ratio, respectively
         */
        pair<double, double>* node_ea(Node* node) {
            pair<double, double>* EA;
            if(node->is_leaf()) {
                EA = new pair<double, double>(node->get_image()->get_relative_area(), node->get_image()->get_aspect_ratio());
            } else {
                pair<double, double>* leftEA = node_ea(node->get_left());
                pair<double, double>* rightEA = node_ea(node->get_right());
                double relativeArea;
                double aspectRatio;
                if(node->get_orientation() == VERTICAL) {
                    double maxRelativeHeight = max(sqrt(leftEA->first*leftEA->second), sqrt(rightEA->first*rightEA->second));
                    double totalRelativeWidth = sqrt(leftEA->first / leftEA->second) + sqrt(rightEA->first / rightEA->second);
                    relativeArea = maxRelativeHeight * totalRelativeWidth;
                    aspectRatio = maxRelativeHeight / totalRelativeWidth;
                } else {
                    double totalRelativeHeight = sqrt(leftEA->first * leftEA->second) + sqrt(rightEA->first * rightEA->second);
                    double maxRelativeWidth = max(sqrt(leftEA->first / leftEA->second), sqrt(rightEA->first / rightEA->second));
                    relativeArea = maxRelativeWidth * totalRelativeHeight;
                    aspectRatio = totalRelativeHeight / maxRelativeWidth;
                }
                EA = new pair<double, double>(relativeArea, aspectRatio);
            }
            return EA;
        }
    public:
        virtual double score(Node* node) {
//            if(node->get_width(alpha, beta) > width || node->get_height(alpha, beta) > height)
//                return 0.0;
            double aPage = height / (double)width;
            pair<double, double>* EA = node_ea(node);
            double propE = (node->sum_relative_areas() / EA->first);
            double propA = (min(EA->second, aPage) / max(EA->second, aPage));
            double score = propE * propA;
            return score;
        }
};
